import axios, { AxiosRequestConfig } from 'axios'
import { API_URL, LOGIN_TOKEN_KEY } from '../config'
import { cloneDeep } from 'lodash'

export interface Options {
    method : 'POST' | 'GET' | 'DELETE';
    data? : any;
    params? : object;
}

export interface Result {
    success : boolean;
    code? : number;
    data? : any;
    msg : string;
}

class Http {
    private sending:boolean = false;

    async post(url: string, data?: any, params? : object)  : Promise<Result>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.post(url, data, params)
        this.sending = false
        return result
    }

    static post(url: string, data?: any, params? : object) : Promise<Result> {
        let options:Options = {
            method : 'POST'
        }
        if(data) {
            options.data = data
            // if(data instanceof FormData) {
            //     options.processData = false
            // }
        }

        if(params) {
            options.params = params
        }

        return Http.send(url, options)
    }

    async get(url: string, params? : object, data?: any) : Promise<Result>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.get(url, params, data)
        this.sending = false
        return result
    }

    static get(url: string, params? : object, data?: any) : Promise<Result>{
        let options:Options = {
            method : 'GET'
        }

        if(params) {
            options.params = params
        }

        if(data) {
            options.data = data
        }

        return Http.send(url, options)
    }

    async delete(url: string, data?: any, params? : object) : Promise<Result>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.delete(url, data, params)
        this.sending = false
        return result
    }

    static delete(url: string, data?: any, params? : object) : Promise<Result>{
        let options:Options = {
            method : 'DELETE'
        }

        if(params) {
            options.params = params
        }

        if(data) {
            options.data = data
        }

        return Http.send(url, options)
    }

    static async send(url: string, options: Options) : Promise<Result>{
        try {

            let config:AxiosRequestConfig = {
                baseURL : API_URL,
                url,
                ...options,
                responseType : 'json',
                headers : {
                    
                }
            }

            //let token = localStorage.getItem(LOGIN_TOKEN_KEY)
            let token = 'eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MjQyNDEzMzAsInN1YiI6IjEifQ.lgnQkquA_jsrprTU3Jchjl6SL8JxtUuZJ-hfhMmee7k'
            if(token) {
                config.headers.Authorization = token
            }

            let response = await axios(config)


            let result:Result = {
                msg : '',
                success : false
            }

            if(response.data) {
                result.code = response.data.code
                result.data = response.data.data
                result.msg = response.data.message
            }

            result.success = result.code == 200 || result.code == 0

            // if(result.data && typeof result.data === 'object') {
            //     Http._change(result.data)
            // }
    
            return result
            
        } catch (error) {
            return {
                success : false,
                code : 404,
                msg : error.message
            }
        }
    }


    private static _change(data : any) {
        if(typeof data !== 'object') return

        for(let k in data) {
            let keyType = typeof k
            let valueType = typeof data[k]
            if(keyType === 'string') { 
                if(k.includes('_')) { //需要转驼峰
                    let tmpValue = cloneDeep(data[k])
                    delete data[k]
                    k = Http._hump(k)
                    data[k] = tmpValue
                }
            }

            if(valueType === 'object') {
                Http._change(data[k])
            }
        }

    }

    private static _hump(name: string) {
        name = name.replace(/_(\w)/g, function($0, $1) {
            return $1.toUpperCase();
        })

        return name
    }
}

export default Http